<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>Erlang 备忘清单
 &#x26;  erlang cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="Erlang 是一种用于构建并发、分布式和容错系统的编程语言。以下是一些常用的命令和操作。

入门，为开发人员分享快速参考备忘单。">
<meta keywords="erlang,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="../style/style.css">
<link rel="stylesheet" href="../style/katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="../index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/erlang.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.5.5"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="erlang-备忘清单"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 256 225">
<g fill="none" fill-rule="evenodd">
<path fill="#A90533" fill-rule="nonzero" d="M44.3399607 159.659089C25.536634 139.734178 14.5349706 112.207841 14.5628518 79.365069 14.5368716 50.3198413 23.6628914 25.3541383 39.3517627 6.35691059L39.3264162 6.36641554 6.35057459 6.36641554 6.35057459 159.662257 44.3165152 159.662257 44.3399607 159.659089 44.3399607 159.659089ZM218.009347 159.671762C226.109466 150.996277 233.367446 140.779089 239.944238 129.094336L203.445228 110.844831C190.626852 131.684119 171.881188 150.865742 145.959287 150.993742 108.232872 150.865742 93.4102182 118.60594 93.4916246 77.0832472L234.469386 77.0832472C234.657584 72.3954056 234.657584 70.2156037 234.469386 67.9591284 235.389466 43.2557621 228.842456 22.4912472 216.932753 6.32079178L216.871287 6.36704921 248.612753 6.36704921 248.612753 159.662891 217.940278 159.662891 218.009347 159.671762 218.009347 159.671762Z"></path>
<path fill="#A90533" fill-rule="nonzero" d="M95.7737825,41.4967126 C97.3344954,22.6971878 112.156515,10.0543363 129.535367,10.0163165 C147.032713,10.0543363 159.674931,22.6971878 160.103287,41.4967126 L95.7737825,41.4967126 L95.7737825,41.4967126 Z"></path>
<polygon fill="currentColor" fill-rule="nonzero" points="26.426 185.668 26.426 179.281 6.807 179.281 6.807 217.149 26.426 217.149 26.426 210.761 14.107 210.761 14.107 200.724 25.969 200.724 25.969 194.337 14.107 194.337 14.107 185.668"></polygon>
<path fill="currentColor" fill-rule="nonzero" d="M59.7310102,201.179881 C64.6216241,200.453703 67.3070894,195.607446 67.4870498,190.686416 C67.3070894,182.635723 62.088238,179.304555 54.7123964,179.280475 L44.6751687,179.280475 L44.6751687,217.148198 L51.9749706,217.148198 L51.9749706,202.092356 L61.0997231,217.148198 L70.2244756,217.148198 L59.7310102,201.179881 L59.7310102,201.179881 Z M51.974337,185.212198 L52.8868122,185.212198 C56.8693865,185.361743 59.8583766,186.270416 59.7303766,191.143287 C59.8583766,195.608079 56.9707726,196.819644 52.8868122,196.618139 L51.974337,196.618139 L51.974337,185.212198 L51.974337,185.212198 Z"></path>
<polygon fill="currentColor" fill-rule="nonzero" points="93.036 179.281 85.737 179.281 85.737 217.149 103.529 217.149 103.529 210.761 93.036 210.761"></polygon>
<path fill="currentColor" fill-rule="nonzero" d="M140.940674,209.392158 L144.134337,217.148198 L151.890377,217.148198 L137.74701,178.368 L131.815921,178.368 L116.76008,217.148198 L124.515485,217.148198 L127.709783,209.392158 L140.940674,209.392158 L140.940674,209.392158 Z M139.115723,203.461703 L129.990971,203.461703 L134.097109,189.318337 L139.115723,203.461703 L139.115723,203.461703 Z"></path>
<polygon fill="currentColor" fill-rule="nonzero" points="165.578 217.149 173.334 217.149 173.334 191.6 193.409 218.061 198.883 218.061 198.883 179.281 191.127 179.281 191.127 204.83 171.052 178.368 165.578 178.368"></polygon>
<path fill="currentColor" fill-rule="nonzero" d="M230.820119,197.073743 L230.820119,203.004832 L239.032396,203.004832 C238.861307,207.771248 234.960476,211.809584 230.363882,211.673347 C223.104634,211.809584 219.507327,204.794297 219.414179,198.442455 C219.507327,192.175525 223.053941,184.856713 230.363882,184.755327 C234.20008,184.856079 237.443169,187.480713 239.032396,190.686416 L245.419723,187.492753 C242.610693,181.575604 236.936238,178.294495 230.363882,178.368 C219.051089,178.295129 211.806416,187.632792 211.658139,198.442455 C211.806416,208.982812 218.848951,218.371168 229.907644,218.516911 C241.850931,218.371168 247.373307,208.831366 247.244674,197.986218 L247.244674,197.073743 L230.820119,197.073743 L230.820119,197.073743 L230.820119,197.073743 Z"></path>
</g>
</svg><a aria-hidden="true" tabindex="-1" href="#erlang-备忘清单"><span class="icon icon-link"></span></a>Erlang 备忘清单</h1><div class="wrap-body">
<p><a href="https://www.erlang.org/">Erlang</a> 是一种用于构建并发、分布式和容错系统的编程语言。以下是一些常用的命令和操作。</p>
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#启动-erlang-shell">启动 Erlang Shell</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#编译代码">编译代码</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#运行代码">运行代码</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#退出-erlang-shell">退出 Erlang Shell</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#代码结构">代码结构</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#模块定义">模块定义</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#导出函数">导出函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#注释">注释</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#变量">变量</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#数据类型">数据类型</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#原子">原子</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数字">数字</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#布尔值">布尔值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串">字符串</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#元组">元组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#列表">列表</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字典-map">字典 (Map)</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#控制结构">控制结构</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#条件语句">条件语句</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#case-表达式">case 表达式</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#函数定义">函数定义</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#列表操作">列表操作</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#列表生成">列表生成</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#并发">并发</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#启动进程">启动进程</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#发送消息">发送消息</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#接收消息">接收消息</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#模式匹配">模式匹配</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#常用内置函数-bifs">常用内置函数 (BIFs)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#列表操作-1">列表操作</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#元组操作">元组操作</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串操作">字符串操作</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#文件操作">文件操作</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#列表操作-2">列表操作</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串操作-1">字符串操作</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#文件操作-1">文件操作</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#示例简单的服务器">示例：简单的服务器</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#并发编程">并发编程</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#创建进程">创建进程</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#发送消息-1">发送消息</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#接收消息-1">接收消息</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#链接进程">链接进程</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#监控进程">监控进程</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#错误处理">错误处理</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#捕获异常">捕获异常</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#常见异常类型">常见异常类型</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#错误处理-1">错误处理</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#分布式编程">分布式编程</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#启动分布式节点">启动分布式节点</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#连接节点">连接节点</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#发送消息到远程节点">发送消息到远程节点</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#otp-框架">OTP 框架</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#定义-genserver">定义 GenServer</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用-genserver">使用 GenServer</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#测试">测试</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#编写-eunit-测试">编写 EUnit 测试</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#运行-eunit-测试">运行 EUnit 测试</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="启动-erlang-shell"><a aria-hidden="true" tabindex="-1" href="#启动-erlang-shell"><span class="icon icon-link"></span></a>启动 Erlang Shell</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">erl
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="编译代码"><a aria-hidden="true" tabindex="-1" href="#编译代码"><span class="icon icon-link"></span></a>编译代码</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 在 Erlang Shell 中编译</span>
</span><span class="code-line">c<span class="token punctuation">(</span>module<span class="token punctuation">)</span>.
</span><span class="code-line"><span class="token comment"># 在命令行中编译</span>
</span><span class="code-line">erlc module.erl
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="运行代码"><a aria-hidden="true" tabindex="-1" href="#运行代码"><span class="icon icon-link"></span></a>运行代码</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 在 Erlang Shell 中运行</span>
</span><span class="code-line">module:function<span class="token punctuation">(</span><span class="token punctuation">)</span>.
</span><span class="code-line"><span class="token comment"># 从命令行运行</span>
</span><span class="code-line">erl <span class="token parameter variable">-noshell</span> <span class="token parameter variable">-s</span> module <span class="token keyword">function</span> <span class="token parameter variable">-s</span> init stop
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="退出-erlang-shell"><a aria-hidden="true" tabindex="-1" href="#退出-erlang-shell"><span class="icon icon-link"></span></a>退出 Erlang Shell</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">q<span class="token punctuation">(</span><span class="token punctuation">)</span>.
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="代码结构"><a aria-hidden="true" tabindex="-1" href="#代码结构"><span class="icon icon-link"></span></a>代码结构</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="模块定义"><a aria-hidden="true" tabindex="-1" href="#模块定义"><span class="icon icon-link"></span></a>模块定义</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token operator">-</span><span class="token function">module</span><span class="token punctuation">(</span><span class="token atom">module_name</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token operator">-</span><span class="token function">export</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token atom">function_name</span><span class="token operator">/</span><span class="token atom">arity</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">function_name</span><span class="token punctuation">(</span><span class="token variable">Args</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">    <span class="token comment">% Function body.</span>
</span><span class="code-line">    <span class="token variable">Result</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="导出函数"><a aria-hidden="true" tabindex="-1" href="#导出函数"><span class="icon icon-link"></span></a>导出函数</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token operator">-</span><span class="token function">export</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token atom">function1</span><span class="token operator">/</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token atom">function2</span><span class="token operator">/</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="注释"><a aria-hidden="true" tabindex="-1" href="#注释"><span class="icon icon-link"></span></a>注释</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token comment">% 单行注释</span>
</span><span class="code-line"><span class="token comment">% 这是一个注释</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="变量"><a aria-hidden="true" tabindex="-1" href="#变量"><span class="icon icon-link"></span></a>变量</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token variable">VarName</span> <span class="token operator">=</span> <span class="token variable">Value</span><span class="token punctuation">.</span> <span class="token comment">% 变量名必须以大写字母开头</span>
</span><span class="code-line"><span class="token variable">Age</span> <span class="token operator">=</span> <span class="token number">25.</span>
</span><span class="code-line"><span class="token variable">Name</span> <span class="token operator">=</span> <span class="token string">"Alice"</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="数据类型"><a aria-hidden="true" tabindex="-1" href="#数据类型"><span class="icon icon-link"></span></a>数据类型</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="原子"><a aria-hidden="true" tabindex="-1" href="#原子"><span class="icon icon-link"></span></a>原子</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token atom">atom</span><span class="token punctuation">.</span>       <span class="token comment">% 例子：atom, 'Atom with spaces'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="数字"><a aria-hidden="true" tabindex="-1" href="#数字"><span class="icon icon-link"></span></a>数字</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token number">123.</span>        <span class="token comment">% 整数</span>
</span><span class="code-line"><span class="token number">3.14</span><span class="token punctuation">.</span>       <span class="token comment">% 浮点数</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="布尔值"><a aria-hidden="true" tabindex="-1" href="#布尔值"><span class="icon icon-link"></span></a>布尔值</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token boolean">true</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token boolean">false</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="字符串"><a aria-hidden="true" tabindex="-1" href="#字符串"><span class="icon icon-link"></span></a>字符串</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token string">"Hello, World!"</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="元组"><a aria-hidden="true" tabindex="-1" href="#元组"><span class="icon icon-link"></span></a>元组</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token punctuation">{</span><span class="token atom">ok</span><span class="token punctuation">,</span> <span class="token string">"Success"</span><span class="token punctuation">}</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="列表"><a aria-hidden="true" tabindex="-1" href="#列表"><span class="icon icon-link"></span></a>列表</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token variable">H</span><span class="token punctuation">|</span><span class="token variable">T</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span> <span class="token comment">% H = 1, T = [2, 3]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="字典-map"><a aria-hidden="true" tabindex="-1" href="#字典-map"><span class="icon icon-link"></span></a>字典 (Map)</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token punctuation">#</span><span class="token punctuation">{</span><span class="token atom">key1</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token atom">value1</span><span class="token punctuation">,</span> <span class="token atom">key2</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token atom">value2</span><span class="token punctuation">}</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="控制结构"><a aria-hidden="true" tabindex="-1" href="#控制结构"><span class="icon icon-link"></span></a>控制结构</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="条件语句"><a aria-hidden="true" tabindex="-1" href="#条件语句"><span class="icon icon-link"></span></a>条件语句</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token keyword">if</span>
</span><span class="code-line">    <span class="token variable">Condition1</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">Expression1</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token variable">Condition2</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">Expression2</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token boolean">true</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">DefaultExpression</span>
</span><span class="code-line"><span class="token keyword">end</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="case-表达式"><a aria-hidden="true" tabindex="-1" href="#case-表达式"><span class="icon icon-link"></span></a>case 表达式</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token keyword">case</span> <span class="token variable">Expression</span> <span class="token keyword">of</span>
</span><span class="code-line">    <span class="token variable">Pattern1</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">Expression1</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token variable">Pattern2</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">Expression2</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token variable">_</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">DefaultExpression</span>
</span><span class="code-line"><span class="token keyword">end</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="函数定义"><a aria-hidden="true" tabindex="-1" href="#函数定义"><span class="icon icon-link"></span></a>函数定义</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token comment">% 无参函数</span>
</span><span class="code-line"><span class="token function">my_function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">    <span class="token atom">ok</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">% 有参函数</span>
</span><span class="code-line"><span class="token function">add</span><span class="token punctuation">(</span><span class="token variable">A</span><span class="token punctuation">,</span> <span class="token variable">B</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">    <span class="token variable">A</span> <span class="token operator">+</span> <span class="token variable">B</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="列表操作"><a aria-hidden="true" tabindex="-1" href="#列表操作"><span class="icon icon-link"></span></a>列表操作</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="列表生成"><a aria-hidden="true" tabindex="-1" href="#列表生成"><span class="icon icon-link"></span></a>列表生成</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token comment">% 生成 1 到 10 的列表</span>
</span><span class="code-line"><span class="token punctuation">[</span> <span class="token variable">X</span> <span class="token punctuation">|</span><span class="token punctuation">|</span> <span class="token variable">X</span> <span class="token operator">&#x3C;</span><span class="token operator">-</span> <span class="token atom">lists</span><span class="token punctuation">:</span><span class="token function">seq</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">% 生成 1 到 10 中的偶数</span>
</span><span class="code-line"><span class="token punctuation">[</span> <span class="token variable">X</span> <span class="token punctuation">|</span><span class="token punctuation">|</span> <span class="token variable">X</span> <span class="token operator">&#x3C;</span><span class="token operator">-</span> <span class="token atom">lists</span><span class="token punctuation">:</span><span class="token function">seq</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token variable">X</span> <span class="token operator">rem</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="并发"><a aria-hidden="true" tabindex="-1" href="#并发"><span class="icon icon-link"></span></a>并发</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="启动进程"><a aria-hidden="true" tabindex="-1" href="#启动进程"><span class="icon icon-link"></span></a>启动进程</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token function">spawn</span><span class="token punctuation">(</span><span class="token variable">Module</span><span class="token punctuation">,</span> <span class="token variable">Function</span><span class="token punctuation">,</span> <span class="token variable">Args</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">% 示例</span>
</span><span class="code-line"><span class="token variable">Pid</span> <span class="token operator">=</span> <span class="token function">spawn</span><span class="token punctuation">(</span><span class="token keyword">fun</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token atom">io</span><span class="token punctuation">:</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token string">"Hello from process~n"</span><span class="token punctuation">)</span> <span class="token keyword">end</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="发送消息"><a aria-hidden="true" tabindex="-1" href="#发送消息"><span class="icon icon-link"></span></a>发送消息</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token variable">Pid</span> <span class="token operator">!</span> <span class="token variable">Message</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">% 示例</span>
</span><span class="code-line"><span class="token variable">Pid</span> <span class="token operator">!</span> <span class="token punctuation">{</span><span class="token atom">hello</span><span class="token punctuation">,</span> <span class="token function">self</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="接收消息"><a aria-hidden="true" tabindex="-1" href="#接收消息"><span class="icon icon-link"></span></a>接收消息</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token keyword">receive</span>
</span><span class="code-line">    <span class="token variable">Pattern1</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">Expression1</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token variable">Pattern2</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">Expression2</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token keyword">after</span> <span class="token variable">Timeout</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">TimeoutExpression</span>
</span><span class="code-line"><span class="token keyword">end</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="模式匹配"><a aria-hidden="true" tabindex="-1" href="#模式匹配"><span class="icon icon-link"></span></a>模式匹配</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token punctuation">{</span><span class="token atom">ok</span><span class="token punctuation">,</span> <span class="token variable">Value</span><span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token atom">ok</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">}</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="常用内置函数-bifs"><a aria-hidden="true" tabindex="-1" href="#常用内置函数-bifs"><span class="icon icon-link"></span></a>常用内置函数 (BIFs)</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="列表操作-1"><a aria-hidden="true" tabindex="-1" href="#列表操作-1"><span class="icon icon-link"></span></a>列表操作</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token atom">lists</span><span class="token punctuation">:</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token variable">List1</span><span class="token punctuation">,</span> <span class="token variable">List2</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token atom">lists</span><span class="token punctuation">:</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token variable">Function</span><span class="token punctuation">,</span> <span class="token variable">List</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token atom">lists</span><span class="token punctuation">:</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token variable">Function</span><span class="token punctuation">,</span> <span class="token variable">List</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token atom">lists</span><span class="token punctuation">:</span><span class="token function">foldl</span><span class="token punctuation">(</span><span class="token variable">Function</span><span class="token punctuation">,</span> <span class="token variable">Acc</span><span class="token punctuation">,</span> <span class="token variable">List</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="元组操作"><a aria-hidden="true" tabindex="-1" href="#元组操作"><span class="icon icon-link"></span></a>元组操作</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token function">element</span><span class="token punctuation">(</span><span class="token variable">N</span><span class="token punctuation">,</span> <span class="token variable">Tuple</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token function">setelement</span><span class="token punctuation">(</span><span class="token variable">N</span><span class="token punctuation">,</span> <span class="token variable">Tuple</span><span class="token punctuation">,</span> <span class="token variable">Value</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token function">tuple_size</span><span class="token punctuation">(</span><span class="token variable">Tuple</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="字符串操作"><a aria-hidden="true" tabindex="-1" href="#字符串操作"><span class="icon icon-link"></span></a>字符串操作</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token atom">string</span><span class="token punctuation">:</span><span class="token function">len</span><span class="token punctuation">(</span><span class="token variable">String</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token atom">string</span><span class="token punctuation">:</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token variable">String1</span><span class="token punctuation">,</span> <span class="token variable">String2</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token atom">string</span><span class="token punctuation">:</span><span class="token function">tokens</span><span class="token punctuation">(</span><span class="token variable">String</span><span class="token punctuation">,</span> <span class="token variable">Delimiters</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="文件操作"><a aria-hidden="true" tabindex="-1" href="#文件操作"><span class="icon icon-link"></span></a>文件操作</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token atom">file</span><span class="token punctuation">:</span><span class="token function">read_file</span><span class="token punctuation">(</span><span class="token variable">Filename</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token atom">file</span><span class="token punctuation">:</span><span class="token function">write_file</span><span class="token punctuation">(</span><span class="token variable">Filename</span><span class="token punctuation">,</span> <span class="token variable">Data</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token atom">file</span><span class="token punctuation">:</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token variable">Filename</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="列表操作-2"><a aria-hidden="true" tabindex="-1" href="#列表操作-2"><span class="icon icon-link"></span></a>列表操作</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token atom">lists</span><span class="token punctuation">:</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">fun</span><span class="token punctuation">(</span><span class="token variable">X</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">X</span> <span class="token operator">*</span> <span class="token number">2</span> <span class="token keyword">end</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token atom">lists</span><span class="token punctuation">:</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token keyword">fun</span><span class="token punctuation">(</span><span class="token variable">X</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">X</span> <span class="token operator">rem</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token keyword">end</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="字符串操作-1"><a aria-hidden="true" tabindex="-1" href="#字符串操作-1"><span class="icon icon-link"></span></a>字符串操作</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token atom">string</span><span class="token punctuation">:</span><span class="token function">len</span><span class="token punctuation">(</span><span class="token string">"Hello"</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token atom">string</span><span class="token punctuation">:</span><span class="token function">upper</span><span class="token punctuation">(</span><span class="token string">"hello"</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="文件操作-1"><a aria-hidden="true" tabindex="-1" href="#文件操作-1"><span class="icon icon-link"></span></a>文件操作</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token punctuation">{</span><span class="token atom">ok</span><span class="token punctuation">,</span> <span class="token variable">File</span><span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token atom">file</span><span class="token punctuation">:</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">"test.txt"</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token atom">write</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token atom">file</span><span class="token punctuation">:</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token variable">File</span><span class="token punctuation">,</span> <span class="token string">"Hello, file!"</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token atom">file</span><span class="token punctuation">:</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token variable">File</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="示例简单的服务器"><a aria-hidden="true" tabindex="-1" href="#示例简单的服务器"><span class="icon icon-link"></span></a>示例：简单的服务器</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token operator">-</span><span class="token function">module</span><span class="token punctuation">(</span><span class="token atom">server</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token operator">-</span><span class="token function">export</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token atom">start</span><span class="token operator">/</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token atom">loop</span><span class="token operator">/</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">    <span class="token function">spawn</span><span class="token punctuation">(</span><span class="token keyword">fun</span> <span class="token atom">loop</span><span class="token operator">/</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">loop</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">    <span class="token keyword">receive</span>
</span><span class="code-line">        <span class="token punctuation">{</span><span class="token atom">echo</span><span class="token punctuation">,</span> <span class="token variable">Msg</span><span class="token punctuation">}</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">            <span class="token atom">io</span><span class="token punctuation">:</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token string">"Echo: ~p~n"</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token variable">Msg</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</span><span class="code-line">            <span class="token function">loop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">        <span class="token atom">stop</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">            <span class="token atom">io</span><span class="token punctuation">:</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token string">"Server stopping~n"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</span><span class="code-line">            <span class="token atom">ok</span><span class="token punctuation">;</span>
</span><span class="code-line">        <span class="token variable">_</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">            <span class="token atom">io</span><span class="token punctuation">:</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token string">"Unknown message~n"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</span><span class="code-line">            <span class="token function">loop</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token keyword">end</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="并发编程"><a aria-hidden="true" tabindex="-1" href="#并发编程"><span class="icon icon-link"></span></a>并发编程</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="创建进程"><a aria-hidden="true" tabindex="-1" href="#创建进程"><span class="icon icon-link"></span></a>创建进程</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token variable">Pid</span> <span class="token operator">=</span> <span class="token function">spawn</span><span class="token punctuation">(</span><span class="token variable">Module</span><span class="token punctuation">,</span> <span class="token variable">Function</span><span class="token punctuation">,</span> <span class="token variable">Args</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="发送消息-1"><a aria-hidden="true" tabindex="-1" href="#发送消息-1"><span class="icon icon-link"></span></a>发送消息</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token variable">Pid</span> <span class="token operator">!</span> <span class="token variable">Message</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="接收消息-1"><a aria-hidden="true" tabindex="-1" href="#接收消息-1"><span class="icon icon-link"></span></a>接收消息</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token keyword">receive</span>
</span><span class="code-line">    <span class="token variable">Pattern1</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">Actions1</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token variable">Pattern2</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">Actions2</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token keyword">end</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="链接进程"><a aria-hidden="true" tabindex="-1" href="#链接进程"><span class="icon icon-link"></span></a>链接进程</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token function">link</span><span class="token punctuation">(</span><span class="token variable">Pid</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token function">unlink</span><span class="token punctuation">(</span><span class="token variable">Pid</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="监控进程"><a aria-hidden="true" tabindex="-1" href="#监控进程"><span class="icon icon-link"></span></a>监控进程</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token variable">MonitorRef</span> <span class="token operator">=</span> <span class="token atom">erlang</span><span class="token punctuation">:</span><span class="token function">monitor</span><span class="token punctuation">(</span><span class="token atom">process</span><span class="token punctuation">,</span> <span class="token variable">Pid</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token atom">erlang</span><span class="token punctuation">:</span><span class="token function">demonitor</span><span class="token punctuation">(</span><span class="token variable">MonitorRef</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="错误处理"><a aria-hidden="true" tabindex="-1" href="#错误处理"><span class="icon icon-link"></span></a>错误处理</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="捕获异常"><a aria-hidden="true" tabindex="-1" href="#捕获异常"><span class="icon icon-link"></span></a>捕获异常</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token keyword">try</span> <span class="token variable">Expression</span> <span class="token keyword">of</span>
</span><span class="code-line">    <span class="token variable">Pattern</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">Result</span>
</span><span class="code-line"><span class="token keyword">catch</span>
</span><span class="code-line">    <span class="token variable">Class</span><span class="token punctuation">:</span><span class="token variable">Reason</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">Handler</span>
</span><span class="code-line"><span class="token keyword">end</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="常见异常类型"><a aria-hidden="true" tabindex="-1" href="#常见异常类型"><span class="icon icon-link"></span></a>常见异常类型</h3><div class="wrap-body">
<ul>
<li><code>throw</code></li>
<li><code>error</code></li>
<li><code>exit</code></li>
</ul>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="错误处理-1"><a aria-hidden="true" tabindex="-1" href="#错误处理-1"><span class="icon icon-link"></span></a>错误处理</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token keyword">try</span> <span class="token variable">Expression</span> <span class="token keyword">of</span>
</span><span class="code-line">    <span class="token variable">Pattern</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">Result</span>
</span><span class="code-line"><span class="token keyword">catch</span>
</span><span class="code-line">    <span class="token variable">Type</span><span class="token punctuation">:</span><span class="token variable">Reason</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token variable">ErrorHandlingExpression</span>
</span><span class="code-line"><span class="token keyword">end</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="分布式编程"><a aria-hidden="true" tabindex="-1" href="#分布式编程"><span class="icon icon-link"></span></a>分布式编程</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="启动分布式节点"><a aria-hidden="true" tabindex="-1" href="#启动分布式节点"><span class="icon icon-link"></span></a>启动分布式节点</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">erl <span class="token parameter variable">-name</span> nodename@hostname <span class="token parameter variable">-setcookie</span> Cookie
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="连接节点"><a aria-hidden="true" tabindex="-1" href="#连接节点"><span class="icon icon-link"></span></a>连接节点</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token atom">net_adm</span><span class="token punctuation">:</span><span class="token function">ping</span><span class="token punctuation">(</span><span class="token variable">Node</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="发送消息到远程节点"><a aria-hidden="true" tabindex="-1" href="#发送消息到远程节点"><span class="icon icon-link"></span></a>发送消息到远程节点</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token punctuation">{</span><span class="token atom">remote_process</span><span class="token punctuation">,</span> <span class="token quoted-atom atom">'remote_node@host'</span><span class="token punctuation">}</span> <span class="token operator">!</span> <span class="token variable">Message</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="otp-框架"><a aria-hidden="true" tabindex="-1" href="#otp-框架"><span class="icon icon-link"></span></a>OTP 框架</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="定义-genserver"><a aria-hidden="true" tabindex="-1" href="#定义-genserver"><span class="icon icon-link"></span></a>定义 GenServer</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token operator">-</span><span class="token function">module</span><span class="token punctuation">(</span><span class="token atom">my_gen_server</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token operator">-</span><span class="token function">behaviour</span><span class="token punctuation">(</span><span class="token atom">gen_server</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token operator">-</span><span class="token function">export</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token atom">start_link</span><span class="token operator">/</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token atom">init</span><span class="token operator">/</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token atom">handle_call</span><span class="token operator">/</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token atom">handle_cast</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token atom">handle_info</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token atom">terminate</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token atom">code_change</span><span class="token operator">/</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">start_link</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">    <span class="token atom">gen_server</span><span class="token punctuation">:</span><span class="token function">start_link</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token atom">local</span><span class="token punctuation">,</span> <span class="token variable">?MODULE</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token variable">?MODULE</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">    <span class="token punctuation">{</span><span class="token atom">ok</span><span class="token punctuation">,</span> <span class="token punctuation">#</span><span class="token atom">state</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">handle_call</span><span class="token punctuation">(</span><span class="token variable">Request</span><span class="token punctuation">,</span> <span class="token variable">From</span><span class="token punctuation">,</span> <span class="token variable">State</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">    <span class="token punctuation">{</span><span class="token atom">reply</span><span class="token punctuation">,</span> <span class="token variable">Reply</span><span class="token punctuation">,</span> <span class="token variable">State</span><span class="token punctuation">}</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">handle_cast</span><span class="token punctuation">(</span><span class="token variable">Msg</span><span class="token punctuation">,</span> <span class="token variable">State</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">    <span class="token punctuation">{</span><span class="token atom">noreply</span><span class="token punctuation">,</span> <span class="token variable">State</span><span class="token punctuation">}</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">handle_info</span><span class="token punctuation">(</span><span class="token variable">Info</span><span class="token punctuation">,</span> <span class="token variable">State</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">    <span class="token punctuation">{</span><span class="token atom">noreply</span><span class="token punctuation">,</span> <span class="token variable">State</span><span class="token punctuation">}</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">terminate</span><span class="token punctuation">(</span><span class="token variable">Reason</span><span class="token punctuation">,</span> <span class="token variable">State</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">    <span class="token atom">ok</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">code_change</span><span class="token punctuation">(</span><span class="token variable">OldVsn</span><span class="token punctuation">,</span> <span class="token variable">State</span><span class="token punctuation">,</span> <span class="token variable">Extra</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">    <span class="token punctuation">{</span><span class="token atom">ok</span><span class="token punctuation">,</span> <span class="token variable">State</span><span class="token punctuation">}</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="使用-genserver"><a aria-hidden="true" tabindex="-1" href="#使用-genserver"><span class="icon icon-link"></span></a>使用 GenServer</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token atom">gen_server</span><span class="token punctuation">:</span><span class="token function">start_link</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token atom">local</span><span class="token punctuation">,</span> <span class="token variable">Name</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token variable">Module</span><span class="token punctuation">,</span> <span class="token variable">Args</span><span class="token punctuation">,</span> <span class="token variable">Options</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token atom">gen_server</span><span class="token punctuation">:</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token variable">ServerRef</span><span class="token punctuation">,</span> <span class="token variable">Request</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token atom">gen_server</span><span class="token punctuation">:</span><span class="token function">cast</span><span class="token punctuation">(</span><span class="token variable">ServerRef</span><span class="token punctuation">,</span> <span class="token variable">Msg</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="测试"><a aria-hidden="true" tabindex="-1" href="#测试"><span class="icon icon-link"></span></a>测试</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="编写-eunit-测试"><a aria-hidden="true" tabindex="-1" href="#编写-eunit-测试"><span class="icon icon-link"></span></a>编写 EUnit 测试</h3><div class="wrap-body">
<pre class="language-erlang"><code class="language-erlang code-highlight"><span class="code-line"><span class="token operator">-</span><span class="token function">module</span><span class="token punctuation">(</span><span class="token atom">module_name_tests</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token operator">-</span><span class="token function">include_lib</span><span class="token punctuation">(</span><span class="token string">"eunit/include/eunit.hrl"</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">simple_test</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">    ?<span class="token function">assertEqual</span><span class="token punctuation">(</span><span class="token variable">Expected</span><span class="token punctuation">,</span> <span class="token variable">Actual</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">complex_test_</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span>
</span><span class="code-line">    <span class="token punctuation">[</span>
</span><span class="code-line">        <span class="token punctuation">{</span><span class="token string">"Test case 1"</span><span class="token punctuation">,</span> <span class="token variable">?_assertEqual</span><span class="token punctuation">(</span><span class="token variable">Expected1</span><span class="token punctuation">,</span> <span class="token variable">Actual1</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
</span><span class="code-line">        <span class="token punctuation">{</span><span class="token string">"Test case 2"</span><span class="token punctuation">,</span> <span class="token variable">?_assertEqual</span><span class="token punctuation">(</span><span class="token variable">Expected2</span><span class="token punctuation">,</span> <span class="token variable">Actual2</span><span class="token punctuation">)</span><span class="token punctuation">}</span>
</span><span class="code-line">    <span class="token punctuation">]</span><span class="token punctuation">.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="运行-eunit-测试"><a aria-hidden="true" tabindex="-1" href="#运行-eunit-测试"><span class="icon icon-link"></span></a>运行 EUnit 测试</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 在命令行中运行</span>
</span><span class="code-line">erl <span class="token parameter variable">-eval</span> <span class="token string">"eunit:test(module_name)"</span> <span class="token parameter variable">-s</span> init stop
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="另见"><a aria-hidden="true" tabindex="-1" href="#另见"><span class="icon icon-link"></span></a>另见</h2><div class="wrap-body">
<ul>
<li><a href="https://www.erlang.org/docs">Erlang 官方文档</a></li>
<li><a href="https://learnyousomeerlang.com/content">Erlang 编程书籍</a></li>
</ul>
</div></div><div class="h2wrap-body"></div></div></div><script src="https://giscus.app/client.js" data-repo="jaywcjlove/reference" data-repo-id="R_kgDOID2-Mw" data-category="Q&#x26;A" data-category-id="DIC_kwDOID2-M84CS5wo" data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="bottom" data-theme="dark" data-lang="zh-CN" crossorigin="anonymous" async></script><div class="giscus"></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 <a href="https://wangchujiang.com/#/app" target="_blank">Kenny Wang</a>.</footer></footer><script src="../data.js?v=1.5.5" defer></script><script src="../js/fuse.min.js?v=1.5.5" defer></script><script src="../js/main.js?v=1.5.5" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
